/**
* Projet Chat System
* 
* @author Yann MBOUNGOU et Jean-Christophe VENDOME
* ELEVES INGENIEUR - 4e Année - Informatique et Réseaux
* INSA TOULOUSE, Dept. GEI - 2014 - 2015
*/
package Controller;
import GUI.*;
import ourMessage.*;
import NI.ChatNI;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ChatController {
    private final ChatGUI chatGUI;
    private ChatNI chatNI;
    private File f=null;
    private static int id=1;
    
    /**
    * README :
    * Cette classe est appelée 
    * à chaque fois qu'on lance notre appli, 
    * dans le cas où notre appli émet un signal
    * vers le système distant, ou même lorsque   
    * une des fonctions du chatNI ou du ChatGUI 
    * est appelée par le chatGUI ou chatNI.Elle sert 
    * en réalité d'intermediaire entre les deux 
    * classes.Cf pattern MVC
    */
    
    public ChatController(ChatGUI chatGUI, ChatNI chatNI) 
    {
       this.chatGUI = chatGUI;
       this.chatNI = chatNI;
    }
    

    //***************Connect the localUser********************//
    public void performConnect(String login) 
    {
        System.out.println("performConnect");
        if(chatNI.isClose())
        { 
            this.setChatNI(new  ChatNI());            
        }
        this.sendHello(login);
    }

    public void sendHello(String login)
    {
        Hello hello = new Hello(login, false); 
        try 
        {
            System.out.println("sendHello");
            this.chatNI.sendBroadcastMessage(hello); 
        } 
        catch (IOException ex) 
        {
           System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    

    //***************DisConnect the localUser********************//
    public void performDisConnect() 
    {
        System.out.println("performDisconnect");
        this.sendBye(this.chatGUI.displayLogin());
    }
    
    public void sendBye(String login)
    {
        Bye bye = new Bye(login);
        try 
        {
            System.out.println("sendBye");
            this.chatNI.sendBroadcastMessage(bye);
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
     //***************Send Text Message********************//
    public void performSend(String login,String msg) 
    {
        System.out.println("performSend");
        this.sendMsg(login,msg);
    }
    
    public void sendMsg(String login,String msg)
    {
        Text text = new Text(login,msg,false);
        try 
        {
            System.out.println("sendMsg");
            this.chatNI.sendBroadcastMessage(text);
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
    //***************Send Text Message private********************//
    public void performSendprivate(String login,String msg,InetAddress addr) throws UnknownHostException 
    {
        System.out.println("performSendprivate");
        this.sendMsgprivate(login,msg,addr);
    }
    
    public void sendMsgprivate(String login,String msg, InetAddress addr)
    {
        Text text = new Text(login,msg,true);
        try 
        {
            System.out.println("sendMsgprivate");
            this.chatNI.sendMessage(text,addr);  
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }    
    
    //***************Send HelloAck********************//   
     public void sendHelloAck(InetAddress addr)
    {
        Hello hello = new Hello(this.chatGUI.displayLogin(), true); //  création du msg helloACK
        try 
        {
            System.out.println("sendhelloAck");
            this.chatNI.sendMessage(hello,addr);            
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
    //***************Receive Hello********************//
         public void receiveHello(String login)
    {
        System.out.println("receiveHello");
        InetAddress addr=this.chatNI.getServer().getPacket().getAddress(); 
        this.chatGUI.performList(login,addr);//<=== on met a jour notre user liste
        this.sendHelloAck(addr);//<=== on envoie un msg de type hello ack         
    }
         
    //***************Receive HelloACK********************//      
      public void receiveHelloAck(String login)
    {
        System.out.println("receiveHelloAck "+login);
        InetAddress addr=this.chatNI.getServer().getPacket().getAddress(); 
       this.chatGUI.performList(login,addr);     //<=== on met a jour notre user liste   
    }  
      
    //***************Receive Bye********************//     
      public void receiveBye(String login)
      {
          System.out.println("receiveBye");
          this.chatGUI.performBye(login); //<=== on met a jour notre user liste en supprimant un user
      }
      
      //***************Receive Text********************//     
      public void receiveText(String login,String msg)
      {
          System.out.println("receiveText");
          this.chatGUI.ReceiveMsg(login,msg);
      }
      //***************Receive Text Private********************//     
      public void receiveTextPriv(String login,String msg)
      {
          System.out.println("receiveText Private");          
          this.chatGUI.ReceiveMsgPriv(login,msg);
      }
      //***************Send File********************//
        public void performSendFile(String login,String name) 
        {
            System.out.println("performSendFile");
            this.openconnexion(login, name);       
        }
        public void openconnexion(String login,String name)
        {
           FileTransfertDemand demand=new FileTransfertDemand(login,name,1024, 4041,id);
           try 
            {
                System.out.println("openconnexion");
                this.chatNI.sendBroadcastMessage(demand);            
            } 
            catch (IOException ex) 
            {
                System.err.println("!!ChatCont: IOException ="+ex);
            }
        }
    //***************Send File private********************//
        public void performSendFileprivate(String login,InetAddress addr,File f) 
        {
            this.f=f;
            System.out.println("performSendFile");
            this.openconnexionprivate(login, f.getName(),addr);       
        }
        public void openconnexionprivate(String login,String name,InetAddress addr)
        {
           FileTransfertDemand demand=new FileTransfertDemand(login,name,1024, 4041,id);
           try 
            {
                System.out.println("openconnexion");
                this.chatNI.sendMessage(demand, addr);          
            } 
            catch (IOException ex) 
            {
                System.err.println("!!ChatCont: IOException ="+ex);
            }
        }
    
      //***************Sending File********************//     
      public void receiveConfirmation(InetAddress addr)
      {
          System.out.println("receiveConfirmation");  
          if(chatGUI.getFile_joined()!=null)
          {
              chatNI.sendFileProcess(chatGUI.getFile_joined(),addr);
          }
          else if (f!=null)
          {
              chatNI.sendFileProcess(f,addr);
          }
      }
      
      //***************Receive File********************//     
      public void closeconnexion()
      {
          System.out.println("closeconnexion : fileSentack");          
          chatGUI.fileSentack();
      }
      //***************Receive File********************//     
      public void docloseconnexion(String login)
      {
          System.out.println("docloseconnexion : receivedfile");          
          chatGUI.receivedfile(login);
      }
      //***************Receive Demand Transfert********************//     
      public void receiveDemand(String login,String name,InetAddress addr)
      {
        FileTransfertConfirmation confirm=new FileTransfertConfirmation(login,true,id);
        //id++;
       try 
        {
            System.out.println("receiveDemand");
            this.chatNI.receiveFileProcess(login,name);
            this.chatNI.sendMessage(confirm, addr);            
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
      }

    public void setChatNI(ChatNI chatNI)
    {
        this.chatNI = chatNI;
    }
      
      
}
